package it.univaq.disim.mwt.business.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import it.univaq.disim.mwt.business.TipologieLaboratoriService;
import it.univaq.disim.mwt.business.model.NotaDisciplinare;
import it.univaq.disim.mwt.business.model.TipologiaLaboratorio;
import it.univaq.disim.mwt.business.model.TipologiaNotaDisciplinare;

public class JDBCTipologiaLaboratoriService implements TipologieLaboratoriService {

   TipologiaLaboratorio tipologiaLaboratori = null;

   //FUNZIONA
   @Override
   public TipologiaLaboratorio findById(Long id) {
	
	    Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		
		String sql="select * from tipologie_laboratori where id = ? ";
		
		try {
			connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE", "school", "school");
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setLong(1, id);
			preparedStatement.executeUpdate();	
		    resultSet = preparedStatement.getResultSet();
		    resultSet.next();
		    
		    Long ide = resultSet.getLong("id");
		    String tipologia = resultSet.getString("tipo");
		    tipologiaLaboratori = new TipologiaLaboratorio(ide,tipologia);
		    
		    resultSet.close();
			preparedStatement.close();
			connection.close();
			
			return tipologiaLaboratori ;
		
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}

   //FUNZIONA
	@Override
	public List<TipologiaLaboratorio> findAll() {

		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		
		List<TipologiaLaboratorio> list = new ArrayList<TipologiaLaboratorio>();
		String sql = " select * from tipologie_laboratori ";
		
		try {
			connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:XE", "school", "school");
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.executeQuery();	
			resultSet=preparedStatement.getResultSet();
			
			while(resultSet.next()){
				Long ide = resultSet.getLong("id");
			    String tipologia = resultSet.getString("tipo");
			    tipologiaLaboratori = new TipologiaLaboratorio(ide,tipologia);
	            
	            list.add(tipologiaLaboratori);
			}
			
			resultSet.close();
			preparedStatement.close();
			connection.close();
		
			return list;
			
		} catch (SQLException e) {
			
			e.printStackTrace();
		}

		return null;
	}

}